home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Programming Windows 95 with MFC
/
Programming Windows 95 with MFC (Microsoft Programming Series)(097-0001465)(1996).iso
/
NT
/
CODE
/
CHAP12
/
PALETTEDEMO
/
PALETTEDEMO.CPP
next >
Wrap
C/C++ Source or Header
|
1996-04-05
|
4KB
|
162 lines
//***********************************************************************
//
// PaletteDemo.cpp
//
//***********************************************************************
#include <afxwin.h>
#include "PaletteDemo.h"
#define FONTHEIGHT 72
CMyApp myApp;
/////////////////////////////////////////////////////////////////////////
// CMyApp member functions
BOOL CMyApp::InitInstance ()
{
m_pMainWnd = new CMainWindow;
m_pMainWnd->ShowWindow (m_nCmdShow);
m_pMainWnd->UpdateWindow ();
return TRUE;
}
/////////////////////////////////////////////////////////////////////////
// CMainWindow message map and member functions
BEGIN_MESSAGE_MAP (CMainWindow, CFrameWnd)
ON_WM_CREATE ()
ON_WM_ERASEBKGND ()
ON_WM_PAINT ()
ON_WM_QUERYNEWPALETTE ()
ON_WM_PALETTECHANGED ()
END_MESSAGE_MAP ()
CMainWindow::CMainWindow ()
{
Create (NULL, "Palette Demo");
}
int CMainWindow::OnCreate (LPCREATESTRUCT lpcs)
{
if (CFrameWnd::OnCreate (lpcs) == -1)
return -1;
CClientDC dc (this);
if (dc.GetDeviceCaps (RASTERCAPS) & RC_PALETTE) {
struct {
LOGPALETTE lp;
PALETTEENTRY ape[63];
} pal;
LOGPALETTE* pLP = (LOGPALETTE*) &pal;
pLP->palVersion = 0x300;
pLP->palNumEntries = 64;
for (int i=0; i<64; i++) {
pLP->palPalEntry[i].peRed = 0;
pLP->palPalEntry[i].peGreen = 0;
pLP->palPalEntry[i].peBlue = 255 - (i * 4);
pLP->palPalEntry[i].peFlags = 0;
}
m_palette.CreatePalette (pLP);
}
return 0;
}
BOOL CMainWindow::OnEraseBkgnd (CDC* pDC)
{
CRect rect;
GetClientRect (&rect);
CPalette* pOldPalette;
if ((HPALETTE) m_palette != NULL) {
pOldPalette = pDC->SelectPalette (&m_palette, FALSE);
pDC->RealizePalette ();
}
DoGradientFill (pDC, &rect);
if ((HPALETTE) m_palette != NULL)
pDC->SelectPalette (pOldPalette, FALSE);
return TRUE;
}
void CMainWindow::OnPaint ()
{
CRect rect;
GetClientRect (&rect);
CPaintDC dc (this);
DoDrawText (&dc, &rect);
}
BOOL CMainWindow::OnQueryNewPalette ()
{
if ((HPALETTE) m_palette == NULL) // Shouldn't happen, but
return 0; // let's be sure
CClientDC dc (this);
CPalette* pOldPalette = dc.SelectPalette (&m_palette, FALSE);
UINT nCount;
if (nCount = dc.RealizePalette ())
Invalidate ();
dc.SelectPalette (pOldPalette, FALSE);
return nCount;
}
void CMainWindow::OnPaletteChanged (CWnd* pFocusWnd)
{
if ((HPALETTE) m_palette == NULL) // Shouldn't happen, but
return; // let's be sure
if (pFocusWnd != this) {
CClientDC dc (this);
CPalette* pOldPalette = dc.SelectPalette (&m_palette, FALSE);
if (dc.RealizePalette ())
Invalidate ();
dc.SelectPalette (pOldPalette, FALSE);
}
}
void CMainWindow::DoGradientFill (CDC* pDC, CRect* pRect)
{
CBrush* pBrush[64];
for (int i=0; i<64; i++)
pBrush[i] = new CBrush (PALETTERGB (0, 0, 255 - (i * 4)));
int nWidth = pRect->Width ();
int nHeight = pRect->Height ();
CRect rect;
for (i=0; i<nHeight; i++) {
rect.SetRect (0, i, nWidth, i + 1);
pDC->FillRect (&rect, pBrush[(i * 63) / nHeight]);
}
for (i=0; i<64; i++)
delete pBrush[i];
}
void CMainWindow::DoDrawText (CDC* pDC, CRect* pRect)
{
CFont font;
int nHeight = -((pDC->GetDeviceCaps (LOGPIXELSY) * FONTHEIGHT) / 72);
font.CreateFont (nHeight, 0, 0, 0, FW_BOLD, TRUE, 0, 0,
DEFAULT_CHARSET, OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS,
DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "Times New Roman");
pDC->SetBkMode (TRANSPARENT);
pDC->SetTextColor (RGB (255, 255, 255));
CFont* pOldFont = pDC->SelectObject (&font);
pDC->DrawText ("Hello, MFC", -1, pRect, DT_SINGLELINE | DT_CENTER |
DT_VCENTER);
pDC->SelectObject (pOldFont);
}